package cc.idoone.spring_boot_hello.amqp.direct;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author hemj
 * @since 2017/10/15
 */
@Component
public class DirectRunner implements CommandLineRunner {

	private final RabbitTemplate rabbitTemplate;
	private final Receiver receiver;
	private final ConfigurableApplicationContext context;

	@Autowired
	public DirectRunner(Receiver receiver, RabbitTemplate rabbitTemplate,
						ConfigurableApplicationContext context) {
		this.receiver = receiver;
		this.rabbitTemplate = rabbitTemplate;
		this.context = context;
	}

	@Override
	public void run(String... args) throws Exception {
		System.out.println("Sending message...");
		rabbitTemplate.convertAndSend(DirectConfig.EXCHANGE, DirectConfig.ROUTINGKEY, "Hello1 " + System.currentTimeMillis());
		rabbitTemplate.convertAndSend(DirectConfig.EXCHANGE, DirectConfig.ROUTINGKEY, "Hello2 " + System.currentTimeMillis());
		rabbitTemplate.convertAndSend(DirectConfig.EXCHANGE, DirectConfig.ROUTINGKEY, "Hello3 " + System.currentTimeMillis());

		receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
		//context.close();
	}

}
